package de.hsos.mad;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ResultActivity extends Activity {
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.activity_result);
		final Bundle extras = getIntent().getExtras(); 
		final Intent callback_intent = new Intent(this, MainActivity.class);
		
		if ((extras != null) 
		&&  extras.containsKey (MainActivity.VALUE_KEY) 
		&&  extras.containsKey (MainActivity.CONVERSION_KEY) 
		&&  extras.containsKey(MainActivity.CONVERSION_RATE_KEY)) {
			final Conversion c = new Conversion ();
			c.value = extras.getFloat (MainActivity.VALUE_KEY);
			c.convertEuro = extras.getBoolean (MainActivity.CONVERSION_KEY);
			c.dollarPerEuro = extras.getFloat (MainActivity.CONVERSION_RATE_KEY);
			showResult(c);
			
			callback_intent.putExtra("result", c.result);
			setResult(Activity.RESULT_OK, callback_intent);
			finish();
		}
		else {
			setResult(Activity.RESULT_CANCELED, callback_intent);
			finish();
		}
	}
	
	private void showResult (Conversion c) {
		setTitle("Ergebnis");
		c.convert();
		final TextView txtResult = (TextView)findViewById(R.id.textView1);
		txtResult.setText(txtResult.getText()+" "+String.valueOf(c.result));
	} 
}
